package repository

import (
	"context"
	"strconv"

	"gitee.com/thewalkers/refactoring-code/model"
	"gitee.com/thewalkers/refactoring-code/model/apperrors"
	"github.com/jmoiron/sqlx"
	"go.uber.org/zap"
)

// MysqlPaymentClassificationRepository is data/repository implementation
// of service layer MysqlPaymentClassificationRepository
type MysqlPaymentClassificationRepository struct {
	DB *sqlx.DB
}

// NewEmployeeRepository is a factory for initializing Employee Repository
func NewPaymentClassificationRepository(db *sqlx.DB) model.PaymentClassificationRepository {
	return &MysqlPaymentClassificationRepository{
		DB: db,
	}
}

// Create Classification
func (m *MysqlPaymentClassificationRepository) Create(ctx context.Context, p *model.PaymentClassification) error {
	query := `INSERT INTO payment_classifications(employee_id) VALUES(?)`
	if _, err := m.DB.Exec(query, p.EmployeeID); err != nil {
		zap.L().Error("repository cound not create a payment_classifications( with err: ", zap.Error(err))
		return apperrors.NewInternal()
	}
	return nil
}

// Find PaymentClassification by Employee ID
func (m *MysqlPaymentClassificationRepository) FindByEmlpoyeeID(ctx context.Context, id int64) (*model.PaymentClassification, error) {
	paymentClassification := &model.PaymentClassification{}

	query := `SELECT * FROM payment_classifications WHERE employee_id = ?`

	// we need to actually check errors as it could be something other than not found
	if err := m.DB.Get(paymentClassification, query, id); err != nil {
		zap.L().Error("repository cound not get payment_classifications with err: ", zap.Error(err))
		return paymentClassification, apperrors.NewNotFound("id", strconv.Itoa(int(id)))
	}

	return paymentClassification, nil
}
